package com.shangcheng.service;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shangcheng.entity.TCustomer;
import com.shangcheng.entity.TUsers;
import com.shangcheng.mapper.TUsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class TUsersServiceImpl {
    @Value("${jwt.secretkey}")
    private String secret;
    @Autowired
    private TUsersMapper usersDao;

    @Transactional(
            readOnly = true
    )
    public TUsers queryByUsername(String username) {
        QueryWrapper<TUsers> qw = new QueryWrapper();
        qw.eq("user_name", username);
        return (TUsers) this.usersDao.selectOne(qw);
    }
    public String createTocken(TUsers user) {
        Map<String, Object> headerMap = new HashMap();
        headerMap.put("alg", "HS256");
        headerMap.put("typ", "JWT");
        Map<String, Object> playLoad = new HashMap();
        playLoad.put("id", user.getId());
        playLoad.put("username", user.getUserName());
        playLoad.put("role", "user");
        playLoad.put("exp", String.valueOf(System.currentTimeMillis() + 86400000L));
        String token = JWTUtil.createToken(headerMap, playLoad, this.secret.getBytes());
        return token;
    }
}
